package org.rsbot.script.randoms;

import org.rsbot.script.Random;
import org.rsbot.script.ScriptManifest;
import org.rsbot.script.wrappers.RSInterface;
import org.rsbot.script.wrappers.RSNPC;

import java.awt.*;
import org.rsbot.script.wrappers.RSInterfaceChild;

/**
 * @author Secret Spy
 * @version 2.6 - 02/09/11
 */
@ScriptManifest(authors = {"Secret Spy"}, name = "BeeHive", version = 2.6)
public class BeehiveSolver extends Random {

    RSNPC BeehiveKeeper;
    private static final int BeehiveKeeperID = 8649;
    private static final int[] DestInterfaceIDs = {16, 17, 18, 19};
    private static final int IDDown = 16034;
    private static final int IDMiddown = 16022;
    private static final int IDMidup = 16025;
    private static final int IDTop = 16036;
    private static final int[] BeehiveArrays = {IDTop, IDMidup, IDMiddown, IDDown};
    private static final String[] ModelNames = {"Top", "Middle Up", "Middle Down", "Down"};
    boolean Solved;
    private static final int[] StartInterfaceIDs = {12, 13, 14, 15};
    private static final int InterfaceBeehiveWindow = 420;
    private static final int BuildBeehive = 40;
    private static final int CloseWindow = 38;

    @Override
    public boolean activateCondition() {
        if (!isLoggedIn()) {
            return false;
        }
        if (getNearestNPCByID(BeehiveKeeperID) != null && getNearestObjectByID(16168) != null) {
            Solved = false;
            return true;
        }
        return false;
    }

    public boolean dragInterfaces(final RSInterfaceChild child1, final RSInterfaceChild child2) {
        final Point start = returnMidInterface(child1);
        final Point finish = returnMidInterface(child2);

        moveMouse(start);
        dragMouse(finish);
        return true;
    }

    public RSInterface getBeehiveInterface() {
        return getInterface(420);
    }

    @Override
    public int loop() {
        BeehiveKeeper = getNearestNPCByID(BeehiveKeeperID);
        if (BeehiveKeeper == null) {
            return -1;
        }
        if (myClickContinue()) {
            return 200;
        }
        if (atInterface(236, 2)) {
            return random(800, 1200);
        }
        if (getBeehiveInterface().isValid()) {
            for (int i = 1; i < 5; i++) {
                log.info("Checking ID: " + i);
                final int id = returnIdAtSlot(i);
                dragInterfaces(getBeehiveInterface().getComponent(StartInterfaceIDs[i - 1]), getBeehiveInterface().getComponent(returnDragTo(id)));
            }
            sleep(2000);
            if (getSetting(805) == 109907968) {
                Solved = true;
                log("All bee pieces have been place, now finishing random");
            } else {
                atInterface(InterfaceBeehiveWindow, CloseWindow);
                return random(500, 1000);
            }
            if (Solved && atInterface(InterfaceBeehiveWindow, BuildBeehive)) {
                return random(900, 1600);
            }
        } else {
            log.info("Interfaces not valid.");
        }
        if (getMyPlayer().getInteracting() == null) {
            final RSNPC npc = getNearestNPCByID(BeehiveKeeperID);
            if (npc != null) {
                if (!clickCharacter(npc, "Talk-to")) {
                    setCameraRotation(getCameraAngle() + random(-30, 30));
                }
            }
        }
        return random(500, 1000);
    }

    public boolean myClickContinue() {
        sleep(random(800, 1000));
        return atInterface(243, 7) || atInterface(241, 5) || atInterface(242, 6) || atInterface(244, 8) || atInterface(64, 5);
    }

    public int returnDragTo(final int Model) {
        switch (Model) {
            case 16036:
                return DestInterfaceIDs[0];
            case 16025:
                return DestInterfaceIDs[1];
            case 16022:
                return DestInterfaceIDs[2];
            case 16034:
                return DestInterfaceIDs[3];
            default:
                return -1;
        }
    }

    public int returnIdAtSlot(final int slot) {
        if ((slot < 1) || (slot > 4)) {
            log.info("Invalid Slot.");
            atInterface(InterfaceBeehiveWindow, CloseWindow);
        }
        int Model_ID = getBeehiveInterface().getComponent(returnSlotId(slot)).getModelID();
        if (Model_ID == -1) {
            log.info("Could not retrieve ID. Restarting.");
            atInterface(InterfaceBeehiveWindow, CloseWindow);
        }
        for (int i = 0; i < BeehiveArrays.length; i++) {
            if (Model_ID == BeehiveArrays[i]) {
                log.info("Slot " + slot + " contains section: " + ModelNames[i]);
                return Model_ID;
            }
        }
        return -1;
    }

    public Point returnMidInterface(final RSInterfaceChild child) {
        Point point = new Point(-1, -1);
        final Rectangle rect = child.getArea();
        if (rect != null) {
            point = new Point((int) rect.getCenterX(), (int) rect.getCenterY());
        }
        return point;
    }

    public int returnSlotId(final int slot) {
        switch (slot) {
            case 1:
                return 25;
            case 2:
                return 22;
            case 3:
                return 23;
            case 4:
                return 21;
            default:
                log.info("Invalid slot ID. Restarting.");
                atInterface(InterfaceBeehiveWindow, CloseWindow);
                break;
        }
        return -1;
    }
}
